// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const total = await db.collection('sentence').count();
  var max = total.total;
  var random = parseInt((Math.random() * (max - 1) + 1).toFixed(0));
  //通过JS的方法获取当前年月日
  var date = new Date();
  date = new Date(date.getTime() + 8 * 60 * 60 * 1000);
  //获取年份  
  const Y = date.getFullYear();
  //获取月份  
  const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //获取当日日期 
  const D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  const result = await db.collection('sentence').where({
    sentence_id: random,
  }).get();
  return {
    sentence: result.data[0].sentence,
    author: result.data[0].author,
    date: Y + "." + M + "." + D,
  }
}